package httpMsg;

import httpMsg.model.reInfo.HJFReInfo;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;

public class HJFClient {
	/**
	 * HTTP POST请求
	 * @param url
	 * @param obj
	 * @return
	 */
	public HashMap<String, Object> HJFHttpJsonPost(String url, String action, String json){
		boolean bErrFlag = false;
		
		HashMap<String,Object> mapResult = new HashMap<String,Object>();  
		if((url==null || "".equals(url))&&(json==null)){
			mapResult.put("ErrCode", 10000);
			return mapResult;
		}
		
		// 创建默认的httpClient实例   
        CloseableHttpClient httpclient = HttpClients.createDefault();  
        // 创建httppost    
        HttpPost httppost = new HttpPost(url);  
        try {  
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
            if(action!=null && !"".equals(action)){
            	nameValuePairs.add(new BasicNameValuePair("trade", action)); 
            }
            nameValuePairs.add(new BasicNameValuePair("data", json));  
            httppost.addHeader("Content-type", "application/x-www-form-urlencoded");  
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));  
            
            CloseableHttpResponse response = httpclient.execute(httppost);  
            try{
                if ( 200 == response.getStatusLine().getStatusCode() ) { 
                	 HttpEntity entity = response.getEntity();  
                     if (entity != null) {  
	                	// 读返回数据 
	                    String conResult = EntityUtils.toString(entity, "UTF-8");
	                    HJFReInfo reMsg = JSON.parseObject(conResult, HJFReInfo.class); 
                        mapResult.put("reMsg", reMsg);
                     }
                } else {  
                    String err = response.getStatusLine().getStatusCode()+"";  
                    mapResult.put("ErrCode", err);
                } 
            } catch (UnknownHostException e){
            	bErrFlag = true;
            	e.printStackTrace();
            } finally {
            	response.close();
            }
        } catch (ClientProtocolException e) {  
        	bErrFlag = true;
            e.printStackTrace();  
        } catch (UnsupportedEncodingException e1) { 
        	bErrFlag = true;
            e1.printStackTrace();  
        } catch (IOException e) {
        	bErrFlag = true;
            e.printStackTrace();  
        } finally { 
        	
        	if(bErrFlag){
                mapResult.put("ErrCode", "1111111");
        	}
        	
            // 关闭连接,释放资源    
            try {  
                httpclient.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }   
        return mapResult;  
    }  
}
